package learn.advance.collections1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

class Demo1Collections {
    public static void Test() {
        ArrayList<String> list = new ArrayList<>();

        Collections.addAll(list, "a", "b", "c");
        System.out.println(list);

        Collections.shuffle(list);
        System.out.println(list);
    }
}

class Person implements Comparable<Person> {
    private String name_;
    private int age_;

    public Person() {

    }

    public Person(String name, int age) {
        this.name_ = name;
        this.age_ = age;
    }

    public int GetAge() {
        return age_;
    }

    @Override
    public String toString() {
        return name_ + ":" + age_ + "  ";
    }

    @Override
    public int compareTo(Person o) {
        return this.GetAge() - o.GetAge();
    }
}


class Demo2Sort {
    public static void Test() {
        ArrayList<Integer> list = new ArrayList<>();

        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        System.out.println(list);

        Collections.sort(list);
        System.out.println(list);

        ArrayList<Person> list2 = new ArrayList<>();
        list2.add(new Person("小明", 18));
        list2.add(new Person("小军", 20));
        list2.add(new Person("小红", 17));
        ;

        System.out.println(list2);
        Collections.sort(list2);
        System.out.println(list2);
    }
}


class Student {
    private String name_;
    private int age_;

    public Student() {
    }

    public Student(String name, int age) {
        this.name_ = name;
        this.age_ = age;
    }

    public int GetAge() {
        return age_;
    }

    @Override
    public String toString() {
        return name_ + ":" + age_ + "  ";
    }
}


class Demo3Sort {
    public static void Test() {
        ArrayList<Integer> list1=new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);

        System.out.println(list1);

        Collections.sort(list1, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

        System.out.println(list1);

        ArrayList<Student> list2=new ArrayList<>();
        list2.add(new Student("小明",18));
        list2.add(new Student("小红",17));
        list2.add(new Student("小军",20));

        System.out.println(list2);

        Collections.sort(list2, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.GetAge()-o2.GetAge();
            }
        });

        System.out.println(list2);
    }
}


public class TestCollections {
    public static void main(String[] args) {
        //Demo1Collections.Test();
        //Demo2Sort.Test();
        Demo3Sort.Test();
    }
}
